//#preprocessor

/*
 * File: GlyfTable.java
 * Version: 1.2
 * Initial Creation: May 19, 2010 4:44:05 PM
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package com.sun.pdfview.font.ttf;

//#ifndef BlackBerrySDK4.5.0 | BlackBerrySDK4.6.0 | BlackBerrySDK4.6.1 | BlackBerrySDK4.7.0 | BlackBerrySDK4.7.1
import java.nio.ByteBuffer;
//#else
import com.sun.pdfview.helper.nio.ByteBuffer;
//#endif

/**
 * Model the TrueType Glyf table
 */
public class GlyfTable extends TrueTypeTable
{
	/** 
     * the glyph data, as either a byte buffer (unparsed) or a 
     * glyph object (parsed)
     */
    private Object[] glyphs;
    
    /**
     * The glyph location table
     */
    private LocaTable loca;
    
    /** Creates a new instance of HmtxTable */
    protected GlyfTable(TrueTypeFont ttf)
    {
        super (TrueTypeTable.GLYF_TABLE);
        
        loca = (LocaTable)ttf.getTable("loca");
        
        MaxpTable maxp = (MaxpTable)ttf.getTable("maxp");
        int numGlyphs = maxp.getNumGlyphs();
        
        glyphs = new Object[numGlyphs];
    }
    
    /**
     * Get the glyph at a given index, parsing it as needed
     */
    public Glyf getGlyph(int index)
    {
        Object o = glyphs[index];
        if (o == null)
        {
            return null;
        }
        
        if (o instanceof ByteBuffer)
        {
            Glyf g = Glyf.getGlyf((ByteBuffer)o);
            glyphs[index] = g;
            
            return g;
        }
        else
        {
            return (Glyf)o;
        }
    }
    
    /** get the data in this map as a ByteBuffer */
    public ByteBuffer getData()
    {
        int size = getLength();
        
        ByteBuffer buf = ByteBuffer.allocateDirect(size);
        
        // write the offsets
        int len = glyphs.length;
        for (int i = 0; i < len; i++)
        {
            Object o = glyphs[i];
            if (o == null)
            {
            	continue;
            }
            
            ByteBuffer glyfData = null;
            if (o instanceof ByteBuffer)
            {
                glyfData = (ByteBuffer)o;
            }
            else
            {
                glyfData = ((Glyf)o).getData();
            }
            
            glyfData.rewind();
            buf.put(glyfData);
            glyfData.flip();
        }
        
        // reset the start pointer
        buf.flip();
        
        return buf;
    }
    
    /** Initialize this structure from a ByteBuffer */
    public void setData(ByteBuffer data)
    {
    	int len = glyphs.length;
        for (int i = 0; i < len; i++)
        {
            int location = loca.getOffset(i);
            int length = loca.getSize(i);
            
            if (length == 0)
            {
                // undefined glyph
                continue;
            }
            
            data.position(location);
            ByteBuffer glyfData = data.slice();
            glyfData.limit(length);
            
            glyphs[i] = glyfData;
        }
    }
    
    /**
     * Get the length of this table
     */
    public int getLength()
    {
        int length = 0;
        
        int len = glyphs.length;
        for (int i = 0; i < len; i++)
        {
            Object o = glyphs[i];
            if (o == null)
            {
                continue;
            }
            
            if (o instanceof ByteBuffer)
            {
                length += ((ByteBuffer)o).remaining();
            }
            else
            {
                length += ((Glyf)o).getLength();
            }
        }
        
        return length;
    }
    
    /**
     * Create a pretty String
     */
    public String toString()
    {
        StringBuffer buf = new StringBuffer();
        String indent = "    ";
        
        buf.append(indent + "Glyf Table: (" + glyphs.length + " glyphs)\n");
        buf.append(indent + "  Glyf 0: " + getGlyph(0));
        
        return buf.toString();
    }
}
